﻿//------------------------------------------------------------------------------
// The contents of this file are subject to the Microsoft Public License (Ms-PL)
// You may obtain a copy of the License at http://psadmin.codeplex.com/license. 
// 
// Software distributed under the License is distributed on an "AS IS" basis, 
// WITHOUT WARRANTY OF ANY KIND, either express or implied. 
// See the License for the specific language governing rights and limitations 
// under the License.
// 
// The initial developer is Ben Foster (ben.foster@retroviz.com)
// Copyright (c) Retroviz Limited (http://www.retroviz.com).
// All Rights Reserved.
//------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Security;
using PSAdmin.Core.Interfaces;
using StructureMap;

namespace PSAdmin.Core.Security
{
    public class PSAdminRoleProvider : RoleProvider
    {
        private string _applicationName = "PSAdmin";
        
        #region Properties

        public override string ApplicationName {
            get { return _applicationName;}
            set { _applicationName = value;}
        }

        #endregion

        public override void AddUsersToRoles(string[] usernames, string[] roleNames)
        {
            var _repo = ObjectFactory.GetInstance<IUserRoleRepository>();

            if (usernames.Count() > 0 && roleNames.Count() > 0)
            {
                foreach (var userName in usernames)
                {
                    foreach (var roleName in roleNames)
                    {
                        var role = _repo.GetByName(roleName);
                        if (role != null) {
                            role.Users.Add(new UserRoleMapping(userName.ToLower()));
                            _repo.Save(role);
                        }
                    }
                }
            }
        }

        public override void CreateRole(string roleName)
        {
            var _repo = ObjectFactory.GetInstance<IUserRoleRepository>();
            var role = _repo.GetByName(roleName);

            if (role == null) {
                role = new UserRole(roleName);
                _repo.Save(role);
            }
        }

        public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
        {
            var _repo = ObjectFactory.GetInstance<IUserRoleRepository>();
            var role = _repo.GetByName(roleName);

            if (role != null) {
                _repo.Delete(role);
            }

            return true;
        }

        public override string[] FindUsersInRole(string roleName, string usernameToMatch)
        {
            throw new NotImplementedException();
        }

        public override string[] GetAllRoles()
        {
            var _repo = ObjectFactory.GetInstance<IUserRoleRepository>();

            return (from role in _repo.GetAll()
                    select role.Name).ToArray();
        }

        public override string[] GetRolesForUser(string username) {
            var _repo = ObjectFactory.GetInstance<IUserRoleRepository>();
            return (from role in _repo.GetUserRoles(username.ToLower())
                    select role.Name).ToArray();
        }

        public override string[] GetUsersInRole(string roleName)
        {
            var _repo = ObjectFactory.GetInstance<IUserRoleRepository>();
            return (from user in _repo.GetByName(roleName).Users
                    select user.Username).ToArray();
        }

        public override bool IsUserInRole(string username, string roleName)
        {
            var _repo = ObjectFactory.GetInstance<IUserRoleRepository>();

            var role = _repo.GetUserRoles(username.ToLower())
                            .FirstOrDefault(x => x.Name.Equals(roleName, StringComparison.InvariantCultureIgnoreCase));
            return (role != null);                
        }

        public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
        {
            throw new NotImplementedException();
        }

        public override bool RoleExists(string roleName) {
            var _repo = ObjectFactory.GetInstance<IUserRoleRepository>();
            return (_repo.GetByName(roleName) != null);
        }
    }
}
